#!/bin/bash

THIS_AAF_PACKAGE="aaf-proof"
THIS_DIR=`pwd`
AAF_DIR="`cd ../../../../ ; pwd`"

echo " Installing $THIS_AAF_PACKAGE on `hostname -f` ..."

if [ ! -e $AAF_DIR/aaf.cf ];then
  echo "$AAF_DIR/aaf.cf was not found!!! Skipping postinstall for AAF ..."
  exit 0
fi

source $AAF_DIR/aaf.cf

function SetRootVersion() {
  rm -f $AAF_DIR/root > /dev/null
  if [ "x$ALICE_PROOF_AAF_PROOF_ROOT_SHARED_DIR" == "x" ];then
    ROOT_TAG=`ls -al $ALICE_PROOF_AAF_ALIEN_PACKAGES/VO_ALICE/ROOT/ | grep -v dbg | tail -n 1 | awk '{print $9}'`
    echo "\$ROOT_TAG is $ROOT_TAG ..."
    ln -s   $ALICE_PROOF_AAF_ALIEN_PACKAGES/VO_ALICE/ROOT/$ROOT_TAG/$ROOT_TAG $AAF_DIR/root
  else
    ln -s $ALICE_PROOF_AAF_PROOF_ROOT_SHARED_DIR $AAF_DIR/root
  fi

  rm -f $AAF_DIR/root_dbg > /dev/null
  if [ "x$ALICE_PROOF_AAF_PROOF_ROOT_SHARED_DIR" == "x" ];then
    ROOT_TAG=`ls -al $ALICE_PROOF_AAF_ALIEN_PACKAGES/VO_ALICE/ROOT/ | grep dbg | tail -n 1 | awk '{print $9}'`
    echo "\$ROOT_TAG is $ROOT_TAG ..."
    ln -s   $ALICE_PROOF_AAF_ALIEN_PACKAGES/VO_ALICE/ROOT/$ROOT_TAG/$ROOT_TAG $AAF_DIR/root_dbg
  else
    ln -s ${ALICE_PROOF_AAF_PROOF_ROOT_SHARED_DIR}_dbg $AAF_DIR/root_dbg
  fi
}

function GenerateInitScripts() {

  DISTRIBUTION_NAME=`cat /etc/*-release | awk '{print $1 $2}' | head -n 1`
  if [ "$DISTRIBUTION_NAME" == "ScientificLinux" ]; then
    echo "Scientific Linux found ..."
    GenerateInitScriptsSL
  else
    if [ "$DISTRIBUTION_NAME" == "GentooBase" ]; then
      echo "Gentoo found ..."
      GenerateInitScriptsGentoo
    else
      echo "Not supported Distribution !!!"
      exit 1
    fi
  fi

  echo "This machine is `cat /etc/*-release` ($DISTRIBUTION_NAME)"


}

function GenerateInitScriptsSL() {
  echo "# generated by aaf-proof `date`" > $THIS_DIR/scripts/xproofd.sysconfig
  echo "source $ALICE_PROOF_AAF_DIR/aaf.cf" >> $THIS_DIR/scripts/xproofd.sysconfig
  echo "source $ALICE_PROOF_AAF_DIR/aaf_env.sh" >> $THIS_DIR/scripts/xproofd.sysconfig
  echo "" >> $THIS_DIR/scripts/xproofd.sysconfig
  cat $THIS_DIR/scripts/sl5/xproofd.sysconfig.tmp >> $THIS_DIR/scripts/xproofd.sysconfig

  cp $THIS_DIR/scripts/sl5/xproofd.rc.d $THIS_DIR/scripts/xproofd.rc.d
  chmod +x $THIS_DIR/scripts/xproofd.rc.d
}

function GenerateInitScriptsGentoo() {
  echo "# generated by aaf-proof `date`" > $THIS_DIR/scripts/xproofd.sysconfig
  echo "source $ALICE_PROOF_AAF_DIR/aaf.cf" >> $THIS_DIR/scripts/xproofd.sysconfig
  echo "source $ALICE_PROOF_AAF_DIR/aaf_env.sh" >> $THIS_DIR/scripts/xproofd.sysconfig
  echo "" >> $THIS_DIR/scripts/xproofd.sysconfig
  cat $THIS_DIR/scripts/gentoo/xproofd.conf.d.tmp >> $THIS_DIR/scripts/xproofd.sysconfig

  cp $THIS_DIR/scripts/gentoo/xproofd.init.d $THIS_DIR/scripts/xproofd.rc.d
  chmod +x $THIS_DIR/scripts/xproofd.rc.d
}

function AppendCertificateAndUsernameInGridMapFile() {
  echo "#\"/DC=ch/DC=cern/OU=Organic Units/OU=Users/CN=mvala/CN=665743/CN=Martin Vala\" $ALICE_PROOF_AAF_USER" > $ALICE_PROOF_AAF_PROOF_XPROOFD_DIR/grid-mapfile
  echo "#\"/DC=ch/DC=cern/OU=Organic Units/OU=Users/CN=ganis/CN=393971/CN=Gerardo Ganis\" $ALICE_PROOF_AAF_USER" >> $ALICE_PROOF_AAF_PROOF_XPROOFD_DIR/grid-mapfile
  CERT_SUBJECT=`openssl x509 -in $ALICE_PROOF_AAF_DIR/.globus/usercert.pem -subject | grep subject | awk -F 'subject= ' '{print $2}'`
  echo "\"$CERT_SUBJECT\" $ALICE_PROOF_AAF_USER,pod" >> $ALICE_PROOF_AAF_PROOF_XPROOFD_DIR/grid-mapfile
}


echo "\$ALICE_PROOF_AAF_PROOF_DIR is $ALICE_PROOF_AAF_PROOF_DIR ..."

rm -fR $ALICE_PROOF_AAF_PROOF_DIR/$THIS_AAF_PACKAGE
ln -sf $THIS_DIR $ALICE_PROOF_AAF_PROOF_DIR/$THIS_AAF_PACKAGE

SetRootVersion

rm -Rf $ALICE_PROOF_AAF_PROOF_XPROOFD_DIR
if [ -z "$ALICE_PROOF_AAF_PROOF_XPROOFD_SHARED_DIR" ]; then
	mkdir -p $ALICE_PROOF_AAF_PROOF_XPROOFD_DIR
	cp -R $THIS_DIR/xproofd $ALICE_PROOF_AAF_PROOF_DIR
else
  ln -s $ALICE_PROOF_AAF_PROOF_XPROOFD_SHARED_DIR $ALICE_PROOF_AAF_PROOF_XPROOFD_DIR
fi

echo "export ALICE_PROOF_AAF_DIR=$ALICE_PROOF_AAF_DIR" > $ALICE_PROOF_AAF_DIR/aaf_env.sh
cat $THIS_DIR/scripts/aaf_env.sh >> $ALICE_PROOF_AAF_DIR/aaf_env.sh

GenerateInitScripts 

cp -f $THIS_DIR/scripts/xproofd.sh $ALICE_PROOF_AAF_DIR/scripts/

$THIS_DIR/scripts/xproofd.sh $ALICE_PROOF_AAF_PROOF_DIR/xproofd

if [ ! -z "$ALICE_PROOF_AAF_PROOF_PROOF_CONF_FILE" ];then
  cp -f $ALICE_PROOF_AAF_PROOF_PROOF_CONF_FILE $ALICE_PROOF_AAF_PROOF_XPROOFD_DIR/proof.conf
fi

ls -al $ALICE_PROOF_AAF_PROOF_XPROOFD_DIR/

mkdir -p $ALICE_PROOF_AAF_XROOTD_XRDNAMESPACE/aaf_data &> /dev/null

mkdir -p $ALICE_PROOF_AAF_PROOF_DIR/admin &> /dev/null
mkdir -p $ALICE_PROOF_AAF_PROOF_DIR/dataset/alice/sim &> /dev/null
mkdir -p $ALICE_PROOF_AAF_PROOF_DIR/dataset/alice/data &> /dev/null
mkdir -p $ALICE_PROOF_AAF_PROOF_DIR/proofbox &> /dev/null

AppendCertificateAndUsernameInGridMapFile

# echo ""

echo " DONE installing $THIS_AAF_PACKAGE on `hostname -f` ..."
